import React, { Component } from "react";
import { Switch, Route, Redirect } from "react-router-dom";
import config from "./config.js";
import axios from "axios";
console.log(config, "config------------");

// 单个引入
// import Way from "../view/way/way";
// import Home from "../view/home/home";
// import Css from "../view/c/css/css";
// import Opacity from "../view/c/opacity/opacity";
// import Router from "../view/router/router";

export default class RouterIndex extends Component {
  render() {
    return (
      <Switch>
        {/*  单个引入 */}
        {/* exact 重名 Redirect重定向
                <Route path="/" exact render={()=>(<Redirect to="/home" />)}/>
                 {/* 路由配置 */}
        {/* <Route path="/home" component={Home}/> */}
        {/* 函数组件 */}
        {/* <Route path="/way/:id" component={Way}/> */}
        {/* 路由配置 */}
        {/* <Route path="/css" component={Css}/> */}
        {/* 路由配置 */}
        {/* <Route path="/opacity" component={Opacity}/> */}
        {/* 嵌套路由 */}
        {/* <Route path="/Router" component={Router}/>  */}

        {config.map((item, index) => {
          if (!!item.exact) {
            return (
              <Route
                key={index}
                path={item.path}
                exact
                render={() => <Redirect to="/home" />}
              />
            );
          } else if (item.Childroute && item.Childroute.length > 0) {
            return (
              <Route
                key={index}
                path={item.path}
                // component={item.component}
                render={() => <item.component routes={item.Childroute} />} // 路由组件传参

                // pass the sub-routes down to keep nesting
              />
            ); // props 传参
          } else if (item.path === "/load") {
            return (
              <Route key={index} path={item.path}>
                {/* <item.component promise={fetch("https://api.github.com/search/repositories?q=javascript&sort=stars")} /> */}
                <item.component pro={axios.get("https://api.github.com/search/repositories?q=javascript&sort=stars")} />

              </Route>
            );
          }else if(item.path === "/ajax"){
            return (
              <Route key={index} path={item.path}>
                <item.component source="https://api.github.com/search/repositories?q=javascript&sort=stars" />
              </Route>
            );
          }
          return (
            <Route key={index} path={item.path} component={item.component} />
          );
        })}
      </Switch>
    );
  }
}
